<?php

namespace common\models\auth;

use Yii;
use common\models\BaseModels;

/**
 * This is the model class for table "user_admin_department".
 *
 * @property integer $id
 * @property integer $parentId
 * @property integer $companyId
 * @property string $name
 * @property string $post_title
 * @property string $remark
 * @property integer $isValid
 * @property integer $created_at
 * @property integer $updated_at
 */
class UserAdminDepartment extends BaseModels
{

    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'user_admin_department';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['parentId', 'companyId', 'is_valid', 'created_at', 'updated_at'], 'integer'],
            [['name'], 'required'],
            [['name', 'post_title'], 'string', 'max' => 32],
            [['remark'], 'string', 'max' => 512],
        ];
    }

    /**
     * 获取部门的名称
     */
    public static function getDepartmentName($id){
        $deptInfo = static::findOne($id);
        if ($deptInfo) {
            return $deptInfo->name;
        }
        return "";
    }

    /**
     * 获取所有级别为 Top 1的部门名称
     * dropdownList 
     */
    public static function getTopDepartment(){
        $condition = "is_valid=1 AND parentId=0";
        $topArray  = []; 
        $topDept   = static::find()->where($condition)->select(['id','name'])->orderBy('id desc')->limit(20)->asArray()->all();
        foreach ($topDept as $item) {
            $topArray[$item["id"]] = $item["name"];
        }
        
        return $topArray;
    }

    /**
     * 获取可选合作公司的名称
     */
    public function getTopCompany(){
        return UserAdminCompany::getTopCompanyList();
    }

    /**
     * 获取上一级名称
     * todo : 缓存
     */
    public function getSuperiorTitle($super_id=''){
        $userTitle = static::findOne($super_id);
        $title     = "-";
        if ($userTitle) {
            $title = $userTitle->name;
        }
        return $title;
    }

    /**
     * 获取该部门的公司名称
     * todo : 缓存
     */
    public function getCompanyName($id){
        $userCompany = UserAdminCompany::findOne($id);
        $title = "";
        if ($userCompany) {
            $title = $userCompany->name;
        }
        return $title;
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('app_table', 'ID'),
            'parentId' => Yii::t('app_table', 'admin department parentId'),
            'companyId' => Yii::t('app_table', 'admin department companyId'),
            'name' => Yii::t('app_table', 'admin department name'),
            'post_title' => Yii::t('app_table', 'admin department post'),
            'remark' => Yii::t('app_table', 'remark'),
            'is_valid' => Yii::t('app_table', 'isvalid'),
            'created_at' => Yii::t('app_table', 'created_at'),
            'updated_at' => Yii::t('app_table', 'updated_at'),
        ];
    }
}
